package com.goddess.yeecolor.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.adapter.ExerciseFragmentPagerAdapter;
import com.goddess.yeecolor.base.BaseFragment;
import com.goddess.yeecolor.fragment.ExerciseAll.ExerciseFragmentSinge;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.Titles;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 活动
 */
public class ExerciseFragment extends BaseFragment {
    private ExerciseFragmentPagerAdapter adapter;
    private List<String>mTitle;
    private List<Fragment>mListFragment;

    private ExerciseFragmentSinge mCollege;
    private ExerciseFragment mExercise;
    private MyFragment my;
    private TabLayout mTablayout;
    private ViewPager mViewPager;
    private View mView;

    private Toolbar mToolbar;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mView==null){
            mView=inflater.inflate(R.layout.fragment_exercise,container,false);
            initFindView();
            initData();
        }
        return mView;
    }


    @Override
    public void initOnClck() {

    }

    @Override
    public void initFindView() {
        mTablayout= (TabLayout) mView.findViewById(R.id.mTablayout);
        mViewPager= (ViewPager) mView.findViewById(R.id.mViewPager);
        Titles.initTitle(mView,"优雅荟");
    }
    private  Map<Object,Object>map;
    @Override
    public void initData() {
       map=new HashMap<>();
       setRequst(MyUrl.url+MyUrl.activityType,map,handler,1);
    }

    /**
     * 设置请求接口
     */
    private void setRequst(String url,Map<Object,Object>map,Handler handler,int tag){
        OkttpUtils.getIntence().OkttpPost(url,map,handler,tag);

    }
    private String jsonString;
    private int code;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString=msg.getData().getString("result");
                JSONObject object=new JSONObject(jsonString);
                code=object.getInt("code");
                if (code==100){

                    mTitle=new ArrayList<>();
                    mListFragment=new ArrayList<>();
                    JSONArray result=object.getJSONArray("result");
                    for (int i=0;i<result.length();i++){
                        JSONObject data=result.getJSONObject(i);
                        mTitle.add(data.getString("name"));
                        setFragment(data.getInt("id"));
                    }
                    mTablayout.setTabMode(TabLayout.MODE_FIXED);
                    adapter=new ExerciseFragmentPagerAdapter(getActivity().getSupportFragmentManager(),mListFragment,mTitle);
                    mViewPager.setAdapter(adapter);
                    mTablayout.setupWithViewPager(mViewPager);

                }
                else if (code==403){
                    ToastUtils.toast(object.getString("msg"));
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    };
    private void setFragment(int id){
        mCollege=ExerciseFragmentSinge.newInstance(id);
        mListFragment.add(mCollege);
    }
}
